﻿using System;
using System.Collections.Generic;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Contabil.Communication.Bovespa.Contabil.ContaContabil;

namespace Sinacor.UI.Bovespa.Contabil.Communication
{
    /// <summary>
    /// Communication Class - ContaContabil
    /// </summary>
    public class ContaContabil : BaseCommunication
    {
        #region Construtor

        public ContaContabil(SessionInformation session) : base(session) { }

        #endregion

        #region Métodos Públicos

        /// <summary>
        /// Consultar contas contábeis
        /// </summary>
        /// <param name="parameter">Model.Parameter.FiltroContaContabil</param>
        /// <returns>Coleção de models do tipo Model.ContaContabil</returns>
        public ModelCollection<Model.ContaContabil> ConsultarContasContabeis(Model.Parameter.FiltroContaContabil parameter)
        {
            try
            {
                using (ServiceInstance<IContaContabil> serviceManager = new ServiceInstance<IContaContabil>())
                {
                    IContaContabil servico = serviceManager.TryGetInstance(this.Session);
                    return new Translator.DataContractToModel.ContaContabil().ToContaContabil(servico.ConsultarContasContabeis(new Translator.ModelToDataContract.ContaContabil().ToFiltroContaContabilRequest(parameter)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }
        }

        /// <summary>
        /// Consultar Históricos Contabeis
        /// FGualberto.7COMm - Task: 6312
        /// </summary>
        /// <param name="filtro"></param>
        /// <returns></returns>
        public ModelCollection<Model.HistoricoContabil> ConsultarHistoricosContabeis(Model.Parameter.FiltroHistoricoContabil filtro)
        {
            ModelCollection<Model.HistoricoContabil> response = null;

            try
            {
                using (ServiceInstance<IContaContabil> serviceManager = new ServiceInstance<IContaContabil>())
                {
                    Translator.ModelToDataContract.HistoricoContabil modelToContract = new Sinacor.UI.Bovespa.Contabil.Communication.Translator.ModelToDataContract.HistoricoContabil();
                    FiltroHistoricoContabilRequest req = modelToContract.ToFiltroHistoricoContabilRequest(filtro);

                    IContaContabil servico = serviceManager.TryGetInstance(this.Session);
                    List<HistoricoContabilResponse> responseHistoricoContabil = servico.ConsultarHistoricosContabeis(req);

                    // traduzir para contrato para model
                    Translator.DataContractToModel.HistoricoContabil contractToModel = new Translator.DataContractToModel.HistoricoContabil();
                    response = contractToModel.ToHistoricoContabil(responseHistoricoContabil);
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }

            return response;
        }

        #endregion
    }
}